package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkFix;
import com.pearadmin.modules.sys.domain.dto.WorkUpdateDto;
import com.pearadmin.modules.sys.enums.MangeDictTypeEnum;
import com.pearadmin.modules.sys.service.ISysDictDataService;
import com.pearadmin.modules.sys.service.ISysUserService;
import com.pearadmin.modules.sys.service.IWorkFixService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 检修工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags ={"检修工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "fix")
public class WorkFixController extends BaseController {
    private String prefix = "work/fix" ;

    @Autowired
    private IWorkFixService workFixService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysUserService userService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/fix/main','work:fix:main')")
    public ModelAndView main(Model model) {
        model.addAttribute("users", userService.list(null));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询检修工作记录列表
     */
    @ResponseBody
    @ApiOperation("列表 检修")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/fix/data','work:fix:data')")
    public ResultTable list(@ModelAttribute WorkFix workFix, PageDomain pageDomain) {
        PageInfo<WorkFix> pageInfo = workFixService.selectWorkFixPage(workFix, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增检修工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/fix/add','work:fix:add')")
    public ModelAndView add(Model model) {
        model.addAttribute("fixTypes", dictDataService.selectByCode(MangeDictTypeEnum.FIX.getCode()));
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增检修工作异常图片信息
     */
    @GetMapping("/file/add")
    public ModelAndView fileAdd(@RequestParam("id")String fixId, Model model) {
        model.addAttribute("fixId", fixId);
        return jumpPage(prefix + "/file/addFile");
    }

    /**
     * 新增保存检修工作记录
     */
    @ResponseBody
    @ApiOperation("保存 检修")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/fix/add','work:fix:add')")
    public Result save(@RequestBody WorkFix workFix) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workFix.setCreateTime(LocalDateTime.now());
        workFix.setCreateBy(sysUser.getUserId());
        workFix.setCreateName(sysUser.getRealName());
        workFix.setId(SequenceUtil.makeStringId());
        return decide(workFixService.insertWorkFix(workFix));
    }

    /**
     * 修改检修工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/fix/edit','work:fix:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkFix workFix = workFixService.selectWorkFixById(id);
        workFix.setUpdateTime(LocalDateTime.now());
        mmap.put("workFix", workFix);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存检修工作记录
     */
    @ResponseBody
    @ApiOperation("更新 检修")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/work/fix/edit','work:fix:edit')")
    public Result update(@RequestBody WorkFix workFix) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workFix.setUpdateTime(LocalDateTime.now());
        workFix.setUpdateBy(sysUser.getUserId());
        workFix.setUpdateName(sysUser.getUsername());
        return decide(workFixService.updateWorkFix(workFix));
    }

    /**
     * 更新检修图片信息
     * @return
     */
    @ResponseBody
    @PostMapping("/updateContent")
    public Result updateContent(@RequestBody WorkUpdateDto dto) {
        WorkFix workFix = workFixService.selectWorkFixById(dto.getId());
        workFix.setContent(dto.getContent());
        return decide(workFixService.updateWorkFix(workFix));
    }

    /**
     * 删除检修工作记录
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/fix/remove','work:fix:remove')")
    public Result batchRemove(String ids) {
        return decide(workFixService.deleteWorkFixByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id删除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/fix/remove','work:fix:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workFixService.deleteWorkFixById(id));
    }
}
